using System;
using System.Data;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
using Entity;
using Utilities;


namespace DAL
{
    public class InPmsDataAccess : FreshCmsAccessBase
    {
        #region code CMS
        //public List<InPms> GetInPmsList()
        //{
        //    try
        //    {
        //        const string spName = "spi_InPmss_GetList";

        //        var InPmsList = DbHelper.ExecuteSPList<InPms>(spName);
        //        return InPmsList;
        //    }
        //    catch (Exception exp)
        //    {
        //        LoggerCms.WriteLog(LoggerCms.LogType.Error, exp.ToString());

        //        return new List<InPms>();
        //    }
        //}

        //public List<InPms> GetInPmsListByPaging(int pageIndex, int pageSize, ref int totalRecord)
        //{
        //    try
        //    {
        //        const string spName = "spi_InPmss_GetByPaging";
        //        var sqlParams = new[]
        //                            {
        //                                new SqlParameter("@PageIndex", pageIndex),
        //                                new SqlParameter("@PageSize", pageSize),
        //                                new SqlParameter("@TotalRecord", totalRecord)
        //                            };

        //        SqlCommand sqlCommand = new SqlCommand(spName);
        //        sqlCommand.CommandType = CommandType.StoredProcedure;
        //        sqlCommand.CommandText = spName;
        //        sqlCommand.Parameters.AddRange(sqlParams);
        //        sqlCommand.Parameters["@TotalRecord"].Direction = ParameterDirection.Output;
        //        var InPmsList = DbHelper.ExecuteSqlList<InPms>(sqlCommand);
        //        if (sqlCommand.Parameters["@TotalRecord"].Value != null)
        //        {
        //            totalRecord = (int) sqlCommand.Parameters["@TotalRecord"].Value;
        //        }

        //        return InPmsList;
        //    }
        //    catch (Exception exp)
        //    {
        //        LoggerCms.WriteLog(LoggerCms.LogType.Error, exp.ToString());

        //        return new List<InPms>();
        //    }
        //}

        public List<InPms> GetInPanelListBGroupId(Int32 groupid)
        {
            try
            {
                const string spName = "spi_InPanel_GetByGroupId";
                
                var sqlParams = new[]
                                    {
										new SqlParameter("@GroupId", groupid)
                                    };
                SqlCommand sqlCommand = new SqlCommand(spName);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = spName;
                sqlCommand.Parameters.AddRange(sqlParams);

                var InPmsList = DbHelper.ExecuteSqlList<InPms>(sqlCommand);
                return InPmsList;
            }
            catch (Exception exp)
            {
                LoggerCms.WriteLog(LoggerCms.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return new List<InPms>();
            }
        }

        public InPms GetInPmsById(int pmsId, int panelId)
        {
            try
            {
                const string spName = "spi_InPmss_GetById";
                var sqlParams = new[]
                                    {
										new SqlParameter("@PmsId", pmsId),
                                        new SqlParameter("@PanelId", panelId)
                                    };

                var InPms = DbHelper.ExecuteSPList<InPms>(spName, sqlParams).SingleOrDefault();
                return InPms;
            }
            catch (Exception exp)
            {
                LoggerCms.WriteLog(LoggerCms.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return null;
            }
        }

        public InPms CheckInPms(int panelId, int groupId)
        {
            try
            {
                const string spName = "spi_InPms_CheckPms";
                var sqlParams = new[]
                                    {
										new SqlParameter("@PanelId", panelId),
                                        new SqlParameter("@GroupId", groupId)
                                    };

                var InPms = DbHelper.ExecuteSPList<InPms>(spName, sqlParams).SingleOrDefault();
                return InPms;
            }
            catch (Exception exp)
            {
                LoggerCms.WriteLog(LoggerCms.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return null;
            }
        }

        //public int InsertInPms(InPms InPms)
        //{
        //    try
        //    {
        //        const string spName = "spi_InPmss_Insert";
        //        var sqlParams = new[]
        //                            {

        //                                new SqlParameter("@GroupId", InPms.GroupId),
        //                                new SqlParameter("@PanelId", InPms.PanelId),
        //                                new SqlParameter("@PmsView", InPms.PmsView),
        //                                new SqlParameter("@PmsUpdate", InPms.PmsUpdate),
        //                                new SqlParameter("@PmsDelete", InPms.PmsDelete),
        //                                new SqlParameter("@PmsId", InPms.PmsId),

        //                            };

        //        SqlCommand sqlCommand = new SqlCommand(spName);
        //        sqlCommand.CommandType = CommandType.StoredProcedure;
        //        sqlCommand.CommandText = spName;
        //        sqlCommand.Parameters.AddRange(sqlParams);
        //        sqlCommand.Parameters["@PmsId"].Direction = ParameterDirection.Output;
        //        DbHelper.ExecuteNonQuery(sqlCommand);
        //        if (sqlCommand.Parameters["@PmsId"].Value != null)
        //        {
        //            return (int) sqlCommand.Parameters["@PmsId"].Value;
        //        }

        //        return 0;


        //    }
        //    catch (Exception exp)
        //    {
        //        LoggerCms.WriteLog(LoggerCms.LogType.Error, exp.ToString());

        //        return -1;
        //    }
        //}

        public int UpdateInPms(InPms InPms)
        {
            try
            {
                const string spName = "spi_InPms_Update";
                var sqlParams = new[]
                                    {
                                        new SqlParameter("@GroupId", InPms.GroupId)
                                        , new SqlParameter("@PanelId", InPms.PanelId)
                                        , new SqlParameter("@PmsView", InPms.PmsView)
                                        , new SqlParameter("@PmsUpdate", InPms.PmsUpdate)
                                        , new SqlParameter("@PmsDelete", InPms.PmsDelete)
                                        , new SqlParameter("@PanelSatus", InPms.PanelSatus)
                                    };

                var returnVal = DbHelper.ExecuteInt32SP(spName, sqlParams);

                return returnVal;
            }
            catch (Exception exp)
            {
                LoggerCms.WriteLog(LoggerCms.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return -1;
            }
        }

        //public int DeleteInPms(int pmsId)
        //{
        //    try
        //    {
        //        const string spName = "spi_InPmss_Delete";
        //        var sqlParams = new[]
        //                        {
        //                            new SqlParameter("@PmsId", pmsId)
        //                        };

        //        var returnVal = DbHelper.ExecuteInt32SP(spName, sqlParams);

        //        return returnVal;
        //    }
        //    catch (Exception exp)
        //    {
        //        LoggerCms.WriteLog(LoggerCms.LogType.Error, exp.ToString());

        //        return -1;
        //    }
        //}
		#endregion
    }
}
